
import { post } from '@/utils/http'
import { setStoreUser } from '@/utils/store'

export default {
  namespaced: true,
  state: {
    id: 0,
    token: '',
    nickname: "立即登录",
    //关注的老师的Id
    myAttention: [],
    //订单数量,不做订单页面
    orderCount: 0,
    //账户余额
    balance: 0,
    //今日学习
    todayLearnTime: 0,
    //连续学习天数
    continuous: 0,
    //累计学习时间长
    total: 0,
    // 我的课程列表，课程id
    mysession: [],
    //最近在学
    lately: [],
    // 一周内每天学习时出
    latelyTime: []
  },
  mutations: {
    setUser(state, data) {
      state.id = data.id
      state.nickname = data.nickname
      state.token = data.token
      state.orderCount = data.orderCount
      state.myAttention = data.myAttention
      state.balance = data.balance
      state.todayLearnTime = data.todayLearnTime
      state.continuous = data.continuous
      state.total = data.total
      state.mysession = data.mysession
      state.lately = data.lately
      state.latelyTime = data.latelyTime
    },
    // 用户登录数据持久化操作
    setStore(state, data) {
      setStoreUser(data)
    }
  },
  actions: {
    // 完成用户的登录
    async setUserAction({ commit }, data) {
      try {
        let ret = await post('login', data)
        console.log(ret);console.log(data);
        if (ret.code == 2) return false
        commit('setUser', { ...ret.userData, token:ret.token })
        commit('setStore', { ...ret.userData, token: ret.token })
        return true
      } catch (error) {
        return false
      }
    }
  }
}
